package com.permission.controller;

import com.permission.exception.ParamException;
import com.permission.param.AddAclParam;
import com.permission.param.ReviseAclParam;
import com.permission.service.SysAclService;
import com.permission.util.ResultVoUtil;
import com.permission.util.ValidatorUtil;
import com.permission.vo.ResultVo;
import com.permission.vo.SysAclVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/4/15
 */
@RestController
@RequestMapping("/sys/acl")
public class SysAclController {

    @Autowired
    private SysAclService sysAclService;

    @PostMapping
    public ResultVo add(@RequestBody @Valid AddAclParam addAclParam,
                        BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new ParamException(bindingResult.getFieldError().getDefaultMessage());
        }

        sysAclService.add(addAclParam);
        return ResultVoUtil.success();
    }

    @PutMapping
    public ResultVo update(@RequestBody @Valid ReviseAclParam reviseAclParam,
                           BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new ParamException(bindingResult.getFieldError().getDefaultMessage());
        }

        sysAclService.update(reviseAclParam);
        return ResultVoUtil.success();
    }

    @GetMapping("/all")
    public ResultVo list() {
        List<SysAclVo> sysAclVoList = sysAclService.all();
        return ResultVoUtil.success(sysAclVoList);
    }

    @DeleteMapping
    public ResultVo delete(@RequestParam Integer aclId) {
        int result = sysAclService.delete(aclId);
        return ResultVoUtil.success(result);
    }
}
